package com.cade.agriculturemall.view.personalCenter.editAddress;

import android.media.effect.EffectUpdateListener;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.DeleteCallback;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.SaveCallback;
import com.cade.agriculturemall.base.BasePresenter;
import com.cade.agriculturemall.model.AddressEntity;
import com.cade.agriculturemall.model.UserInfoEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lobby on 2017/12/20.
 */

public class EditAddressPresenter extends BasePresenter<EditAddressContract.IView> implements EditAddressContract.IPresenter {


    public EditAddressPresenter(EditAddressContract.IView view) {
        super(view);
    }

    private AVQuery<AddressEntity> addressEntityAVQuery = new AVQuery<>("user_address");


    @Override
    public void onResume() {

    }

    @Override
    public void onCreate() {

    }

    @Override
    public void onDestroy() {


    }

    @Override
    public void upLoadAddress(String addressId, AddressEntity entity) {
        AVUser.alwaysUseSubUserClass(UserInfoEntity.class);
        final UserInfoEntity user = AVUser.getCurrentUser(UserInfoEntity.class);
        AVObject address = new AVObject("user_address");

        if (addressId != null && !addressId.equals("")) {
            address = AVObject.createWithoutData("user_address", addressId);
        }
        address.put("mobile", entity.getMobile());
        address.put("province", entity.getProvince());
        address.put("city", entity.getCity());
        address.put("county", entity.getCounty());
        address.put("stress", entity.getStress());
        address.put("address", entity.getAddress());
        address.put("ownerObjectId", user);
        address.put("contractor", entity.getContractor());
        address.put("isfirst", entity.getIsfirst());
        address.put("cityId", entity.getCityId());
        address.put("countyId", entity.getCountyId());
        address.saveInBackground(new SaveCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    mView.upLoadSuccess();
                } else {
                    e.printStackTrace();
                    mView.upLoadSuccess();
                }
            }
        });
    }

    @Override
    public void deleteAddress(String addressId) {
        addressEntityAVQuery.whereEqualTo("objectId", addressId);
        addressEntityAVQuery.deleteAllInBackground(new DeleteCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    mView.goBack();
                } else {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void downAddress(String addressId) {
        addressEntityAVQuery.whereEqualTo("objectId", addressId);
        addressEntityAVQuery.findInBackground(new FindCallback<AddressEntity>() {
            @Override
            public void done(List<AddressEntity> list, AVException e) {
                if (e == null) {
                    if (list.size() != 0) {
                        mView.upLoadAddressData(list.get(0));
                    } else {
                        mView.upLoadAddressData(null);
                    }
                } else {
                    e.printStackTrace();
                }
            }
        });
    }
}
